home *** CD-ROM | disk | FTP | other *** search
/ Otherware / Otherware_1_SB_Development.iso / mac / developm / language / harvest.cpt / Harvest C / DMacroCollector.c < prev    next >
Encoding:
C/C++ Source or Header  |  1992-06-16  |  3.0 KB  |  110 lines

  1. /*
  2.     Harvest C
  3.     Copyright 1992 Eric W. Sink.  All rights reserved.
  4.     
  5.     This file is part of Harvest C.
  6.     
  7.     Harvest C is free software; you can redistribute it and/or modify
  8.     it under the terms of the GNU Generic Public License as published by
  9.     the Free Software Foundation; either version 2, or (at your option)
  10.     any later version.
  11.     
  12.     Harvest C is distributed in the hope that it will be useful,
  13.     but WITHOUT ANY WARRANTY; without even the implied warranty of
  14.     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  15.     GNU General Public License for more details.
  16.     
  17.     You should have received a copy of the GNU General Public License
  18.     along with Harvest C; see the file COPYING.  If not, write to
  19.     the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
  20.     
  21.     Harvest C is not in any way a product of the Free Software Foundation.
  22.     Harvest C is not GNU software.
  23.     Harvest C is not public domain.
  24.  
  25.     This file may have other copyrights which are applicable as well.
  26.  
  27. */
  28.  
  29. /*
  30.     DMacroCollector.c
  31. */
  32.  
  33. #include "DMacroCollector.h"
  34. #include "CList.h"
  35. #include "DTokenStream.h"
  36.  
  37. void DMacroCollector::IMacroCollector(DMacro *aMacro)
  38. {
  39.     int i;
  40.     theMacro = aMacro;
  41.     startingParenLevel = -1;    /* DTokenStream will set this */
  42.     currentArg = -1;
  43.     theArgs = (DTokenStream **) icemalloc(theMacro->theParams->GetNumItems() * sizeof(DTokenStream *));
  44.     for (i=0;i<theMacro->theParams->GetNumItems();i++) 
  45.     {
  46.         theArgs[i] = new DTokenStream;
  47.         theArgs[i]->ITokenStream(NULL);    
  48.     }
  49. }
  50.  
  51. void DMacroCollector::Expand(DTokenStream *into,CSymbolList *beingExpanded)
  52. {
  53.     /* Copy the tokens in theMacro->expansion into into.  Add theMacro
  54.     to beingExpanded.  For each token in theMacro->expansion, check to
  55.     see if it is in theMacro->theParams.  If not, into->Add(it).  If so,
  56.     loop through all tokens in the corresponding entry in theArgs[]. */
  57.     int i;
  58.     int j;
  59.     long countTokens;
  60.     long countParamTokens;
  61.     DToken *aToken;
  62.     DSymbol *aParam;
  63.     int argIndex;
  64.     beingExpanded->Append((void *) theMacro);
  65.     countTokens = theMacro->expansion->GetNumItems();
  66.     for (i=1;i<=countTokens;i++) {
  67.         aToken = theMacro->expansion->GetNthToken(i);
  68.         /* Check to see if it is in theMacro->theParams */
  69.         if (aParam = theMacro->theParams->Find(aToken->name)) {
  70.             beingExpanded->Remove((void *) theMacro);
  71.             /* Look up the param in theArgs */
  72.             argIndex = theMacro->theParams->FindIndex((void *) aParam);
  73.             /* Now insert all tokens in theArgs[argIndex] */
  74.             countParamTokens = theArgs[argIndex]->GetNumItems();
  75.             for (j=1;j<=countParamTokens;j++) {
  76.                 aToken = theArgs[argIndex]->GetNthToken(j);
  77.                 into->Add(aToken,beingExpanded);
  78.             }
  79.             beingExpanded->Append((void *) theMacro);
  80.         }
  81.         else {
  82.             into->Add(aToken,beingExpanded);
  83.         }
  84.     }
  85.     beingExpanded->Remove((void *) theMacro);
  86. }
  87.  
  88. void DMacroCollector::StartNextArg()
  89. {
  90.     currentArg++;
  91.     
  92. }
  93.  
  94. void DMacroCollector::AddToArg(DToken *aToken)
  95. {
  96.     theArgs[currentArg]->Add(aToken,NULL);
  97.     
  98. }
  99.  
  100. void DMacroCollector::Dispose(void)
  101. {
  102.     int i;
  103.     for (i=0;i<theMacro->theParams->GetNumItems();i++) 
  104.     {
  105.         theArgs[i]->Dispose();    
  106.     }
  107.     icefree(theArgs);
  108.     inherited::Dispose();
  109. }
  110.